const { ipcMain,shell } = require('electron');
const fs = require('fs');
const path = require('path');
const serverAction = require('./server')();

const replayName = "asynchronous-reply";
let win = null;
let app = null;
let server = null;

const getResourcesPath = () => {
    let resourcesPath = process.resourcesPath;
    if (process.resourcesPath.includes('node_modules')) {
        resourcesPath = "D:\\codes\\node\\electron_web_manager\\pack\\resources";
    }
    return resourcesPath;
};
const getApps = () => {
    let apps = {};
    let appJson = path.join(getResourcesPath(),'apps.json');
    if (fs.existsSync(appJson)) {
        apps = require(appJson);
    }
    return apps;
};
const events = {
    "default"(event,arg) {
        event.reply(replayName,{
            arg,
            msg: '没有该对象可供调用'
        });
    },
    "process"(event,arg) {
        event.reply(replayName, {
            arg,
            msg: {
                resourcesPath: getResourcesPath(),
                execPath: process.execPath
            }
        });
    },
    "apps"(event,arg) {
        event.reply(replayName, {
            arg,
            msg: getApps(),
        });
    },
    "loadApp"(event,arg) {
        let apps = getApps();
        if (arg.app in apps) {
            let newUrl = path.join(getResourcesPath(),apps[arg.app].file,apps[arg.app].index);
            console.log(newUrl);
            win.loadFile(newUrl);
        }
        event.reply(replayName, {
            arg,
            msg: '调用结束'
        });
    },
    "openAppForBrowser"(event,arg) {
        let apps = getApps();
        if (arg.app in apps) {
            // let newUrl = path.join(getResourcesPath(),apps[arg.app].file,apps[arg.app].index);
            // console.log(newUrl);
            // win.loadFile(newUrl);
            if (server) {
                serverAction.start(path.join(getResourcesPath(),apps[arg.app].file));
            } else {
                server = serverAction.start(path.join(getResourcesPath(),apps[arg.app].file),arg.port);
            }
            shell.openExternal("http://localhost:" + arg.port + "/" + apps[arg.app].index);
        }
        event.reply(replayName, {
            arg,
            msg: '调用结束'
        });
    },
    "checkServer"(event,arg) {
        event.reply(replayName, {
            arg,
            msg: !!server
        });
    },
    quit() {
        app.quit();
    },
    devTool(event,arg) {
        win.webContents.openDevTools();
        event.reply(replayName, {
            arg,
            msg: '调用结束'
        });
    },
    resize(event,arg) {
        switch (arg.type) {
            case "maximize":
                win.maximize();
                break;
            case "unmaximize":
                win.unmaximize();
                break;
            case "minimize":
                win.minimize();
                break;
            case "setFullScreen":
                win.setFullScreen(!win.isFullScreen());
                break;
            case "toTray":
                win.hide();
                break;
        }
        event.reply(replayName, {
            arg,
            msg: '调用结束'
        });
    },
};

ipcMain.on('asynchronous-message', (event, arg) => {
    // console.log(arg) // prints "ping"
    // event.reply('asynchronous-reply', 'pong')
    if (typeof arg !== "object") {
        arg = {
            event: arg
        };
    }
    if (arg.event in events) {
        events[arg.event](event,arg);
    } else {
        events.default(event,arg);
    }
});

module.exports = (_win,_app) => {
    win = _win;
    app = _app;
}